package com.pedro.streamer;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.pedro.streamer.fragment.AIRadioFragment;
import com.pedro.streamer.fragment.AIVideoBlankFragment;
import com.pedro.streamer.service.FloatingMediaService;

import java.util.Arrays;


public class NewMainActivity extends BaseMainActivity {


    private static final String TAG = "NewMainActivity";
    private static final int REQUEST_CODE = 1;



    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.ACCESS_MEDIA_LOCATION,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.POST_NOTIFICATIONS};

    private AIVideoBlankFragment aiVideoBlankFragment;

    private AIRadioFragment aiRadioFragment;
    public FragmentManager fragmentManager;

    private Fragment currentFragment;


    @Override
    protected void onPause() {
        super.onPause();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            this.enterPictureInPictureMode();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_main);


        if (!checkPermissions()) {
            ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, REQUEST_CODE);
        }

        fragmentManager = getSupportFragmentManager();

        showFragment(0);


        startFloatingView();
    }

    private void startFloatingView() {
        if (FloatingMediaService.isStarted) {
            return;
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                AlertDialog.Builder builder = new AlertDialog.Builder(NewMainActivity.this);
                builder.setTitle(R.string.permission_lack_warn);
                builder.setMessage("本应用需要悬浮窗权限才是使用全部功能，点击确定授权悬浮窗功能");

                builder.setNegativeButton(R.string.negative, (dialogInterface, i) -> Toast.makeText(NewMainActivity.this, R.string.permission_lack_warn, Toast.LENGTH_SHORT).show());

                builder.setPositiveButton(R.string.positive, (dialogInterface, i) -> startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 416));
                builder.show();
            } else {
                Log.d(TAG, "startService启动悬浮窗服务");
                Intent intent = new Intent(NewMainActivity.this, FloatingMediaService.class);
                startService(intent);
//                MediaPlayerClient.getInstance(this).bindService(intent);
            }
        }
    }

    private boolean checkPermissions() {
        for (String permission : PERMISSIONS_STORAGE) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 416) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(this)) {
                    Toast.makeText(this, "悬浮窗授权失败", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "悬浮窗授权成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(NewMainActivity.this, FloatingMediaService.class);
                    Log.d(TAG, "startService启动悬浮窗服务");
                    startService(intent);
//                    MediaPlayerClient.getInstance(this).bindService(intent);
                }
            }
        }
    }

    private void showFragment(int position){

        switch (position){
            case 0:{

                if (aiVideoBlankFragment == null) {
                    aiVideoBlankFragment = new AIVideoBlankFragment();
                }
                replaceFragment(aiVideoBlankFragment);

                break;


            }



        }


    }

    private void replaceFragment(Fragment fragment) {

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        if (currentFragment != null){
            if (fragment.isAdded()){
                transaction
                        .hide(currentFragment)
                        .show(fragment)
                        .commit();
            }else {
                transaction
                        .hide(currentFragment)
                        .add(R.id.main_fragment, fragment)
                        .commit();
            }
        }else {
            transaction
                    .add(R.id.main_fragment, fragment)
                    .commit();
        }
        currentFragment = fragment;
    }



}